<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-button @click="$router.push('/menu/mat/bom')" size="mini">返回</el-button>
                <el-button v-if="viewOrEdit == '0'" size="mini" @click="showExport">{{$t('export')}}</el-button>
                <el-button v-if="viewOrEdit == '1' && (currentStatus == '未审核' || currentStatus == null)" size="mini" @click="showAuditItem">审核</el-button>
            </div>
        </div>
        <div class="shit-hz">
            <div class="shit-content">
                <div class="shit-menu">
                    <img :ref="'unfold-def' + currentUuid" src="@/assets/unfold.png" alt="卡卡卡" class="unfold-def" @click.stop="showDefCurrentMenu(1)">
                    <img :ref="'unfold-def-pop' + currentUuid" src="@/assets/unfold-pop.png" alt="卡卡卡" class="unfold-def-pop" @click.stop="showDefCurrentMenu(2)">
                    <div class="shit-basic" @click="showTree">{{currentBh + '-' + currentName}}</div>
                </div>
                <el-tree v-if="currentMenu.length > 0" ref="myTreez" :indent="0" :data="currentMenu" node-key="uuid" :expand-on-click-node="false" :props="{ label: 'matName', children: 'matBomTrees' }" @node-click="getCurrentMatData">
                    <span class="custom-tree-node" slot-scope="{ node, data }">
                        <img v-show="data.hasChildren" :ref="'shrink' + data.uuid" src="@/assets/shrink.png" alt="卡卡卡" class="shrink" @click.stop="showCurrentMenu(data, node)">
                        <img v-show="!data.hasChildren" :ref="'unfold' + data.uuid" src="@/assets/unfold.png" alt="卡卡卡" class="unfold" @click.stop="showCurrentMenu(data, node)">
                        <img :ref="'shrink-pop' + data.uuid" src="@/assets/shrink-pop.png" alt="卡卡卡" class="shrink-pop" @click.stop="showCurrentMenu(data, node)">
                        <img :ref="'unfold-pop' + data.uuid" src="@/assets/unfold-pop.png" alt="卡卡卡" class="unfold-pop" @click.stop="showCurrentMenu(data, node)">
                        <span class="matcode-label">{{ data.matCode + '-' + node.label }}</span>
                        <span v-if="viewOrEdit == '1'" :ref="'rm' + data.uuid" class="remove-node" @click.stop="showRemoveCurrentMat(node, data)">
                            <img src="@/assets/remove-bom.png" alt="卡卡卡">
                        </span>
                    </span>
                </el-tree>
            </div>
            <el-card class="cardBox">
                <div class="title-header">
                    基本信息
                </div>
                <div class="button-header-list">
                    <el-button v-if="viewOrEdit == '1'" class="current-add" size="mini" @click="showAddCurrent('')">添加</el-button>
                    <el-button v-if="viewOrEdit == '1'" class="current-cite" size="mini" @click="showCiteCurrent('')">引用</el-button>
                    <el-button v-if="viewOrEdit == '1'" class="current-add" size="mini" @click="showCiteNextCurrent('')">引用下级物料</el-button>
                </div>
                <div class="input-list">
                    <div class="input-item">
                        <div class="template-title">物料编码</div>
                        <el-input v-model="currentItem.bh" size="mini" readonly></el-input>
                    </div>
                    <div class="input-item">
                        <div class="template-title">名称</div>
                        <el-input v-model="currentItem.name" size="mini" readonly></el-input>
                    </div>
                    <div class="input-item">
                        <div class="template-title">尺码</div>
                        <el-input v-model="currentItem.spec" size="mini" readonly></el-input>
                    </div>
                    <div class="input-item">
                        <div class="template-title">颜色</div>
                        <el-input v-model="currentItem.color" size="mini" readonly></el-input>
                    </div>
                    <div class="input-item">
                        <div class="template-title">单价</div>
                        <el-input v-model="currentItem.up" size="mini" readonly></el-input>
                    </div>
                    <div class="input-item" v-if="editItemUsages.usages !== ''">
                        <div class="template-title">用量</div>
                        <el-input v-model="editItemUsages.usages" type="number" size="mini" :readonly="viewOrEdit == '0'"></el-input>
                    </div>
                    <div class="input-item">
                        <div class="template-title">单位</div>
                        <el-input v-model="currentItem.unit" size="mini" readonly></el-input>
                    </div>
                </div>
                <div class="button-list" v-if="editItemUsages.usages !== '' && viewOrEdit == '1'">
                    <el-button class="tab-button" size="mini" @click="showpdateBomNum">保存</el-button>
                </div>
            </el-card>
        </div>
        <!-- 添加物料 -->
        <el-dialog :title="$t('add')" :visible.sync="addBomMatDialogVisible" width="600px" class="addNfRobeDlog" :close-on-click-modal="false" @close="addDlogClose(addBomMatForm)">
            <el-form :model="addBomMatForm" :ref="addBomMatForm" :rules="addBomMatRules" label-position="left" label-width="80px">
                <el-form-item label="物料编码" prop="matterId">
                    <el-input class="styleInput" v-model="addMatItemName" disabled readonly />
                    <el-button class="styleBtn" size="mini" @click="showMatListDlog">选择</el-button>
                </el-form-item>
                <el-form-item label="用量" prop="usages">
                    <el-input type="number" class="styleInput" v-model="addBomMatForm.usages" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addBomMatDialogVisible = false">返回</el-button>
                <el-button type="primary" @click="addData()">保存</el-button>
            </span>
        </el-dialog>
        <!-- 套装引用物料 -->
        <el-dialog title="引用" :visible.sync="citeBomMatDialogVisible" width="600px" class="addNfRobeDlog" :close-on-click-modal="false" @close="citeDlogClose()">
            <el-form :model="citeBomMatForm" :rules="citeBomMatRules" label-position="left" label-width="80px">
                <el-form-item label="物料编码" prop="matterId">
                    <el-input class="styleInput" v-model="citeMatItemName" disabled />
                    <el-button class="styleBtn" size="mini" @click="showMatListDlog">选择</el-button>
                </el-form-item>
                <el-form-item label="用量" prop="usages">
                    <el-input type="number" class="styleInput" v-model="citeBomMatForm.usages" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="citeBomMatDialogVisible = false">返回</el-button>
                <el-button type="primary" @click="citeData()">保存</el-button>
            </span>
        </el-dialog>
        <!-- 引用下级物料 -->
        <el-dialog title="引用下级物料" :visible.sync="citeNextBomMatDialogVisible" width="600px" class="addNfRobeDlog" :close-on-click-modal="false" @close="citeNextDlogClose(citeNextBomMatForm)">
            <el-form :model="citeNextBomMatForm" :ref="citeNextBomMatForm" :rules="citeNextBomMatRules" label-position="left" label-width="80px">
                <el-form-item label="物料编码" prop="matCode">
                    <el-input class="styleInput" v-model="citeMatItemName" disabled />
                    <el-button class="styleBtn" size="mini" @click="showMatListDlog">选择</el-button>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="citeNextBomMatDialogVisible = false">返回</el-button>
                <el-button type="primary" @click="citeNextData()">保存</el-button>
            </span>
        </el-dialog>
        <!-- 物理编码列表 -->
        <el-dialog title="编码列表" :visible.sync="matlistFabDialogVisible" width="1135px" class="styleDlog" @close="matListClose">
            <div class="operarea">
                <div class="seach">
                    <el-input v-model="seachCodeForm.bh" :placeholder="$t('encoding')" clearable size="mini" @keyup.enter.native="seachCodeData"></el-input>
                    <el-input v-model="seachCodeForm.name" :placeholder="$t('name')" clearable size="mini" @keyup.enter.native="seachCodeData"></el-input>
                    <el-input v-model="seachCodeForm.color" :placeholder="$t('color')" clearable size="mini" @keyup.enter.native="seachCodeData"></el-input>
                    <el-button size="mini" icon="el-icon-search" @click="seachCodeData"></el-button>
                </div>
                <div class="btnList">
                    <el-button size="mini" @click="switchAddMode">{{$t('add')}}</el-button>
                </div>
            </div>
            <el-card class="cardBox">
                <el-table style="width: 100%" ref="codeRuleTable" :data="codeRuleTab" highlight-current-row @current-change="handleBomIdChange">
                    <el-table-column prop="bh" min-width="130" :label="$t('encoding')">
                    </el-table-column>
                    <el-table-column prop="name" min-width="120" :label="$t('name')">
                    </el-table-column>
                    <el-table-column prop="color" min-width="120" :label="$t('color')">
                    </el-table-column>
                    <el-table-column prop="spec" min-width="120" :label="$t('parameter')">
                    </el-table-column>
                    <el-table-column prop="up" min-width="120" :label="$t('unitprice')">
                    </el-table-column>
                    <el-table-column prop="unit" min-width="120" :label="$t('unit')">
                    </el-table-column>
                    <el-table-column prop="createTime" min-width="140" :label="$t('time')">
                    </el-table-column>
                </el-table>
                <el-pagination
                    small
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page.sync="seachCodeForm.pageNum"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="seachCodeForm.pageSize"
                    layout="sizes, prev, pager, next"
                    :total="totalCode">
                </el-pagination>
            </el-card>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="jayCheck">已选择</el-button>
            </span>
        </el-dialog>
        <!-- bom列表 -->
        <el-dialog title="bom表" :visible.sync="bomlistFabDialogVisible" width="1135px" class="styleDlog" @close="matListClose">
            <div class="operarea">
                <div class="seach">
                    <el-input v-model="seachBomForm.bh" :placeholder="$t('encoding')" clearable size="mini" @keyup.enter.native="getCiteBomData"></el-input>
                    <el-input v-model="seachBomForm.name" :placeholder="$t('name')" clearable size="mini" @keyup.enter.native="getCiteBomData"></el-input>
                    <el-input v-model="seachBomForm.color" :placeholder="$t('color')" clearable size="mini" @keyup.enter.native="getCiteBomData"></el-input>
                    <el-input v-model="seachBomForm.spec" placeholder="尺码" clearable size="mini" @keyup.enter.native="getCiteBomData"></el-input>
                    <el-button size="mini" icon="el-icon-search" @click="getCiteBomData"></el-button>
                </div>
            </div>
            <el-card class="cardBox">
                <el-table style="width: 100%" ref="bomeTable" :data="bomTab" stripe  highlight-current-row @current-change="handleBomIdChange">
                    <el-table-column prop="bh" min-width="130" :label="$t('encoding')">
                    </el-table-column>
                    <el-table-column prop="name" min-width="120" :label="$t('name')">
                    </el-table-column>
                    <el-table-column prop="color" min-width="120" :label="$t('size')">
                    </el-table-column>
                    <el-table-column prop="color" min-width="120" :label="$t('color')">
                    </el-table-column>
                    <el-table-column prop="reviewStatus" min-width="120" label="审核状态">
                    </el-table-column>
                </el-table>
                <el-pagination
                    small
                    @size-change="handleBomSizeChange"
                    @current-change="handleBomCurrentChange"
                    :current-page.sync="seachBomForm.pageNum"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="seachBomForm.pageSize"
                    layout="sizes, prev, pager, next"
                    :total="bomTotal">
                </el-pagination>
            </el-card>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="coleCheck">已选择</el-button>
            </span>
        </el-dialog>
        <!-- 物理编码添加 -->
        <el-dialog title="添加" :visible.sync="addItemDialogVisible" width="1000px" class="afterDlog" :close-on-click-modal="false" @close="addItemDlogClose()">
            <div class="dialogBox">
                <div class="addcode">
                    <div class="vibeItem">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in firstColumn" :key="item.code" @click="getNextEncode(1, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(1)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="secondFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in secondColumn" :key="item.code" @click="getNextEncode(2, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(2)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="thirdFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in thirdColumn" :key="item.code" @click="getNextEncode(3, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(3)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="fourthFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in fourthColumn" :key="item.code" @click="getNextEncode(4, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(4)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="fifFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in fifColumn" :key="item.code" @click="getNextEncode(5, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(5)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                </div>
                <div class="buttonOp">
                    <el-button class="createButton" type="primary" @click="createCode">生 成</el-button>
                </div>
                <div class="cut-off"></div>
                <template v-if="showParamFlag">
                    <div class="paramInput">
                        <div class="paramHead">
                            <div class="hdItem">{{$t('encoding')}}</div>
                            <div class="hdItem">{{$t('name')}}</div>
                            <div class="hdItem">{{$t('color')}}</div>
                            <div class="hdItem">{{$t('specification')}}</div>
                            <div class="hdItem">品牌</div>
                            <div class="hdItem">库存</div>
                            <div class="hdItem">{{$t('unitprice')}}</div>
                            <div class="hdItem">{{$t('unit')}}</div>
                        </div>
                        <div class="paramtd">
                            <div class="tdItem">{{addLiveType.bh}}</div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.name" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.color" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.spec" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.brand" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.stk" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.up" />
                            </div>
                            <div class="tdItem">
                                <el-select v-model="addLiveType.unit" clearable :placeholder="$t('plssel')">
                                    <el-option label="m" value="m" v-if="keyValue.indexOf('E') == 0 || keyValue.indexOf('F') == 0 "></el-option>
                                    <el-option label="pcs" value="pcs" v-if="keyValue.indexOf('F') == -1"></el-option>
                                </el-select>
                            </div>
                        </div>
                    </div>
                    <div class="addition">
                        <div class="bz">
                            <div class="bzItem">备注</div>
                            <div class="bzItems">
                                <el-input v-model="addLiveType.remark" type="textarea" />
                            </div>
                        </div>
                        <div class="tp">
                            <div class="tpItem">{{$t('image')}}</div>
                            <div class="tpItems">
                                <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="hadleRemove" :limit="1" :on-exceed="scotland" ref="sunsetstrip">
                                    <el-button size="mini" type="primary">点击上传</el-button>
                                </el-upload>
                            </div>
                        </div>
                    </div>
                </template>
            </div>
            <div class="buttonOp" v-if="showParamFlag">
              <el-button class="createButton" type="primary" size="mini" @click="saveModeCode()">{{$t('save')}}</el-button>  
            </div>
        </el-dialog>
        <!-- 修改用量 -->
        <el-dialog :title="$t('hint')" :visible.sync="updateDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>确定修改当前物料的用量吗？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updateDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="updateBomNum">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除引入物料 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="removeCurrentMat">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 撤销套装引入物料 -->
        <el-dialog :title="$t('hint')" :visible.sync="revocationDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>是否删除当前引用物料？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="revocationDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="revocationCurrentMat">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 导出excel表 -->
        <el-dialog :title="$t('export')" :visible.sync="exportDialogVisible" width="420px" class="smallDlog">
            <span>{{$t('whetoexptabdata')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="exportDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="exportData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 审核相关物料是否填写完成 -->
        <el-dialog title="审核" :visible.sync="auditDialogVisible" width="420px" class="smallDlog">
            <span>是否确认当前所需物料填写完成？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="auditDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="auditItem">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    updateBomUsages,
    getMatCode,
    addParMatCode,
    getMaxNumFromChild,
    updateParMatCode,
    insertMatDetail,
    addBomMsg,
    deleteBomUsages,
    bomExcel,
    updateReviewStatus,
    importClotheAndPants,
    cancelBind,
    getBomTreeMsg,
    getDetailMsg,
    importSimilarMatBom,
    getCodeRuleAll,
    checkIsUsed
} from '@/libs/reqlist.js'
export default {
    data() {
        var isletter = (rule, value, callback) => {
            var pattern = /^([A-Z]{1})?$/
            if (!pattern.test(value)) {
                return callback(new Error('请输入一位大写字母'))
            } else {
                callback()
            }
        }
        var isKong = (rule, value, callback) => {
            var pattern = value - 0
            if (pattern == 0) {
                return callback(new Error('编码不能全为0'))
            } else {
                callback()
            }
        }
        return {
            // 判断是查看还是编辑
            viewOrEdit: '',
            // 当前物料审核状态
            currentStatus: '',
            // 当前点击列表对象
            currentItem: {},
            currentBh: '',
            currentName: '',
            currentUuid: '',
            currentLevel: 0,
            currentParentCode: '',
            // 当前点击左侧菜单数据
            currentClickMenu: {},
            // 当前物料左侧菜单
            currentMenu: [],
            // 当前点击tree的哪一个
            currentIconCode: '',
            // 当前点击tree的hasChildren值
            currentHasChild: false,
            // 当前点击tree是否展开
            currentExpanded: false,
            // 上一次点击
            lastClickBh: 'basicInfo',
            // 判断是否为多级展示
            hasChildrenMat: false,
            // 用量
            updateDialogVisible: false,
            editItemUsages: {
                matCode: '',
                rootCode: '',
                usages: '',
                currentCode: ''
            },
            // 添加物料
            addMatItemName: '',
            addBomMatForm: {
                rootCode: '',
                parentCode: '',
                matCode: '',
                level: 1,
                usages: '',
                nodeType: 0,
                batchName: '',
                currentCode: ''
            },
            addBomMatRules: {
                matterId: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur'}
                ],
                usages: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur'}
                ]
            },
            addBomMatDialogVisible: false,
            seachCodeForm: {
                bh: '',
                name: '',
                type: '',
                brand: '',
                uuid: '',
                delete_status: '',
                pageNum: 1,
                pageSize: 10
            },
            totalCode: 0,
            codeRuleTab: [],
            matlistFabDialogVisible: false,
            matEncode: {
                level: 1,
                type: '',
                parentNode: '',
            },
            // 引用下级物料
            citeNextBomMatDialogVisible: false,
            citeNextBomMatForm: {
                rootCode: '',
                matCode: '',
                currentCode: ''
            },
            citeNextBomMatRules: {
                matCode: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur'}
                ]
            },
            // 删除物料id
            delDialogVisible: false,
            deleteCurrentMatId: {
                rootCode: '',
                parentCode: '',
                matCode: '',
                batchName: '',
                currentCode: ''
            },
            currentDelData: null,
            // 编码相关
            addDialogVisible: false,
            // 各子级是否显示
            secondFlag: false,
            thirdFlag: false,
            fourthFlag: false,
            fifFlag: false,
            // 各节点数据
            firstColumn: [],
            secondColumn: {},
            thirdColumn: [],
            fourthColumn: [],
            fifColumn: [],
            // 上一个被点击元素的ref值和level
            lastClickNode:'',
            lastClickLevel: '',
            addBaseDialogVisible: false,
            addBaseRootCode: {
                code: '',
                name: '',
                level: 1,
                type: '',
            },
            addBaseRules: {
                code: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
                    { validator: isletter, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ],
                type: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ]
            },
            addRootCode: {
                code: '',
                name: '',
                level: '',
                type: '',
            },
            addRules: {
                code: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
                    { validator: isKong, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ],
                type: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ]
            },
            addItemDialogVisible: false,
            // 当前level下选中的值
            firstValue: '',
            secondValue: '',
            thirdValue: '',
            fourthValue: '',
            fifValue: '',
            // 各级选择节点parentNo记录
            secondParNode: '',
            thirdParNode: '',
            fourthParNode: '',
            fifthParNode: '',
            // 记录每层被点击node
            firClickNode: '',
            secClickNode: '',
            thrClickNode: '',
            fourClickNode: '',
            fifClickNode: '',
            // 编码相关参数框是否展示
            showParamFlag: false,
            addLiveType: {
                name: '',
                bh: '',
                color: '',
                spec: '',
                brand: '',
                type: '',
                up: '',
                unit: '',
                pictureUrl: '',
                remark: '',
                stk: ''
            },
            // bom信息导出
            exportDialogVisible: false,
            bomExport: {
                bh: '',
                name: '',
                spec: '',
                color: ''
            },
            // 审核
            auditDialogVisible: false,
            auditCommitForm: {
                bomCode: '',
                reviewStatus: 1 
            },
            // 判断是添加还是引用
            addOrCite: '',
            // 引用
            citeMatItemName: '',
            citeBomMatDialogVisible: false,
            citeBomMatForm: {
                rootCode: '',
                parentCode: '',
                matCode: '',
                nodeType: 1,
                level: 1,
                usages: '',
                currentCode: ''
            },
            citeBomMatRules: {
                matterId: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ]
            },
            revocationDialogVisible: false,
            revocationUuid: {
                uuid: '',
                parentBh: ''
            },
            // 查询bom列表
            seachBomForm: {
                bh: '',
                name: '',
                type: '',
                brand: '',
                uuid: '',
                delete_status: '',
                pageNum: 1,
                pageSize: 10
            },
            bomTotal: 0,
            bomTab: [],
            bomlistFabDialogVisible: false
        }
    },
    methods: {
        // 获取树状菜单
        getCurrentBomTree() {
            getBomTreeMsg(JSON.stringify({ rootCode: this.currentBh })).then(res => {
                if (res.data.code == 0) {
                    this.currentMenu = res.data.data
                }
            })
        },
        showTree() {
            this.currentItem = JSON.parse(sessionStorage.getItem('matCodeItem'))
            this.editItemUsages.usages = ''
            switch (this.currentIconCode) {
                case '':
                    break
                default:
                    this.$refs['rm' + this.currentIconCode].style.display = 'none'
            }
            this.currentClickMenu = {}
        },
        showDefCurrentMenu() {
            this.$refs['unfold-def-pop' + this.currentUuid].style.display = 'inline-block'
            this.$refs['unfold-def' + this.currentUuid].style.display = 'none'
            this.showTree()
        },
        showCurrentMenu(data, node) {
            switch (this.$refs.myTreez.store.nodesMap[data.uuid].expanded) {
                case true:
                    this.$refs.myTreez.store.nodesMap[data.uuid].expanded = false
                    break
                case false:
                    this.$refs.myTreez.store.nodesMap[data.uuid].expanded = true
                    break
            }
            switch (this.currentIconCode) {
                case '':
                    this.$refs['rm' + data.uuid].style.display = 'block'
                    if (data.hasChildren && node.expanded == false) {
                        this.$refs['shrink' + data.uuid].style.display = 'none'
                        this.$refs['unfold' + data.uuid].style.display = 'none'
                        this.$refs['unfold-pop' + data.uuid].style.display = 'inline-block'
                    }
                    if (!data.hasChildren || node.expanded == true) {
                        this.$refs['shrink' + data.uuid].style.display = 'none'
                        this.$refs['unfold' + data.uuid].style.display = 'none'
                        this.$refs['unfold-pop' + data.uuid].style.display = 'inline-block'
                    }
                    this.currentIconCode = data.uuid
                    this.currentHasChild = data.hasChildren
                    this.currentExpanded = node.expanded
                    break
                default:
                    // 清除上一个点击菜单图标选中
                    if (this.currentHasChild && this.currentExpanded == false) {
                        this.$refs['shrink-pop' + this.currentIconCode].style.display = 'none'
                        this.$refs['shrink' + this.currentIconCode].style.display = 'inline-block'
                        this.$refs['rm' + this.currentIconCode].style.display = 'none'
                    }
                    if (!this.currentHasChild && this.currentExpanded == true) {
                        this.$refs['unfold-pop' + this.currentIconCode].style.display = 'none'
                        this.$refs['unfold' + this.currentIconCode].style.display = 'inline-block'
                        this.$refs['rm' + this.currentIconCode].style.display = 'none'
                    }
                    if (this.currentHasChild && this.currentExpanded == true) {
                        this.$refs['unfold-pop' + this.currentIconCode].style.display = 'none'
                        this.$refs['unfold' + this.currentIconCode].style.display = 'inline-block'
                        this.$refs['rm' + this.currentIconCode].style.display = 'none'
                    }
                    if (!this.currentHasChild && this.currentExpanded == false) {
                        this.$refs['unfold-pop' + this.currentIconCode].style.display = 'none'
                        this.$refs['unfold' + this.currentIconCode].style.display = 'inline-block'
                        this.$refs['rm' + this.currentIconCode].style.display = 'none'
                    }
                    // 赋予当前点击菜单图标选中
                    if (data.hasChildren && node.expanded == false) {
                        this.$refs['shrink' + data.uuid].style.display = 'none'
                        this.$refs['unfold' + data.uuid].style.display = 'none'
                        this.$refs['shrink-pop' + data.uuid].style.display = 'inline-block'
                        this.$refs['unfold-pop' + data.uuid].style.display = 'none'
                        this.$refs['rm' + data.uuid].style.display = 'block'
                    }
                    if (!data.hasChildren && node.expanded == true) {
                        this.$refs['shrink' + data.uuid].style.display = 'none'
                        this.$refs['unfold' + data.uuid].style.display = 'none'
                        this.$refs['shrink-pop' + data.uuid].style.display = 'none'
                        this.$refs['unfold-pop' + data.uuid].style.display = 'inline-block'
                        this.$refs['rm' + data.uuid].style.display = 'block'
                    }
                    if (!data.hasChildren && node.expanded == false) {
                        this.$refs['shrink' + data.uuid].style.display = 'none'
                        this.$refs['unfold' + data.uuid].style.display = 'none'
                        this.$refs['shrink-pop' + data.uuid].style.display = 'none'
                        this.$refs['unfold-pop' + data.uuid].style.display = 'inline-block'
                        this.$refs['rm' + data.uuid].style.display = 'block'
                    }
                    if (data.hasChildren && node.expanded == true) {
                        this.$refs['shrink' + data.uuid].style.display = 'none'
                        this.$refs['unfold' + data.uuid].style.display = 'none'
                        this.$refs['shrink-pop' + data.uuid].style.display = 'none'
                        this.$refs['unfold-pop' + data.uuid].style.display = 'inline-block'
                        this.$refs['rm' + data.uuid].style.display = 'block'
                    }
                    this.currentIconCode = data.uuid
                    this.currentHasChild = data.hasChildren
                    this.currentExpanded = node.expanded
                    break
            }
        },
        // 当前物料详细信息
        getCurrentMatData (data, node) {
            getDetailMsg({ uuid: data.uuid }).then(res => {
                if (res.data.code == 0) {
                    this.currentParentCode = data.parentCode
                    this.currentLevel = data.level
                    this.currentItem = res.data.data
                    this.editItemUsages.usages = res.data.data.usages
                    this.currentClickMenu = data
                    switch (this.currentIconCode) {
                        case '':
                            this.$refs['rm' + data.uuid].style.display = 'block'
                            if (data.hasChildren && node.expanded == false) {
                                this.$refs['shrink-pop' + data.uuid].style.display = 'inline-block'
                                this.$refs['shrink' + data.uuid].style.display = 'none'
                            }
                            if (!data.hasChildren || node.expanded == true) {
                                this.$refs['unfold-pop' + data.uuid].style.display = 'inline-block'
                                this.$refs['unfold' + data.uuid].style.display = 'none'
                            }
                            this.currentIconCode = data.uuid
                            this.currentHasChild = data.hasChildren
                            this.currentExpanded = node.expanded
                            break
                        default:
                            this.$refs['rm' + this.currentIconCode].style.display = 'none'
                            this.$refs['rm' + data.uuid].style.display = 'block'
                            if (this.currentHasChild && this.currentExpanded == false) {
                                this.$refs['shrink-pop' + this.currentIconCode].style.display = 'none'
                                this.$refs['shrink' + this.currentIconCode].style.display = 'inline-block'
                            }
                            if (!this.currentHasChild || this.currentExpanded == true) {
                                this.$refs['unfold-pop' + this.currentIconCode].style.display = 'none'
                                this.$refs['unfold' + this.currentIconCode].style.display = 'inline-block'
                            }
                            if (data.hasChildren && node.expanded == false) {
                                this.$refs['shrink-pop' + data.uuid].style.display = 'inline-block'
                                this.$refs['shrink' + data.uuid].style.display = 'none'
                            }
                            if (!data.hasChildren || node.expanded == true) {
                                this.$refs['unfold-pop' + data.uuid].style.display = 'inline-block'
                                this.$refs['unfold' + data.uuid].style.display = 'none'
                            }
                            this.currentIconCode = data.uuid
                            this.currentHasChild = data.hasChildren
                            this.currentExpanded = node.expanded
                            break
                    }
                }
            })
        },
        handleNodeClick() {},
        // 添加物料信息
        showAddCurrent() {
            checkIsUsed(JSON.stringify({ rootCode: this.currentBh })).then(res => {
                if (res.data.code == 0) {
                    switch (res.data.data) {
                        case 0:
                            break
                        case 1:
                            this.$message.warning(res.data.msg)
                            break
                    }
                }
            })
            if (this.currentClickMenu.matCode) {
                this.addBomMatForm.rootCode = this.currentClickMenu.matCode
                this.addBomMatForm.parentCode = this.currentClickMenu.matCode
                this.addBomMatForm.currentCode = this.currentBh
                this.addBomMatForm.batchName = this.currentClickMenu.batchName
                this.addBomMatForm.level = 1
            } else {
                this.addBomMatForm.rootCode = this.currentBh
                this.addBomMatForm.parentCode = this.currentBh
                this.addBomMatForm.currentCode = this.currentBh
                this.addBomMatForm.level = 1
            }
            this.addOrCite = 'add'
            this.showMatListDlog()
        },
        addDlogClose(form) {
            this.addMatItemName = ''
            this.addBomMatForm.usages = ''
            this.addOrCite = ''
            this.$refs[form].resetFields()
        },
        addData() {
            if (this.addMatItemName == '') return this.$message.warning('物料不能为空')
            if (this.addBomMatForm.usages == '') return this.$message.warning('用量不能为空')
            addBomMsg(JSON.stringify(this.addBomMatForm)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('添加成功')
                    this.addBomMatDialogVisible = false
                    this.getCurrentBomTree()
                }
            })
        },
        getNewClotheMat() {
            getCodeRuleAll(JSON.stringify(this.seachCodeForm)).then(res => {
                if (res.data.code == 0) {
                    this.totalCode = res.data.data.hits
                    this.codeRuleTab = res.data.data.data
                }
            })
        },
        seachCodeData() {
            this.seachCodeForm.pageNum = 1
            this.getNewClotheMat()
        },
        showMatListDlog() {
            this.getNewClotheMat()
            this.matlistFabDialogVisible = true
        },
        handleSizeChange (newSize) {
            this.seachCodeForm.pageSize = newSize
            this.getNewClotheMat()
        },
        handleCurrentChange (newNum) {
            this.seachCodeForm.pageNum = newNum
            this.getNewClotheMat()
        },
        handleDropIdChange(row) {
            if (!row) return
            switch (this.addOrCite) {
                case 'add':
                    this.addBomMatForm.matCode = row.bh
                    this.addMatItemName = row.bh
                    break
                case 'cite':
                    this.citeBomMatForm.matCode = row.bh
                    this.citeMatItemName = row.bh
                    break
            }
        },
        matListClose() {
            switch (this.addOrCite) {
                case 'add':
                    this.addBomMatDialogVisible = true
                    break
                case 'import':
                    this.citeNextBomMatDialogVisible = true
                    break
                case 'cite':
                    this.citeBomMatDialogVisible = true
            }
            this.seachCodeForm.bh = ''
            this.seachCodeForm.name = ''
            this.seachCodeForm.color = ''
            this.seachCodeForm.pageNum = 1
            this.seachCodeForm.pageSize = 10
        },
        // 更新用量
        showpdateBomNum() {
            this.editItemUsages.rootCode = this.currentParentCode
            this.editItemUsages.matCode = this.currentItem.bh
            this.editItemUsages.currentCode = this.currentBh
            checkIsUsed(JSON.stringify({ rootCode: this.currentItem.bh })).then(res => {
                if (res.data.code == 0) {
                    switch (res.data.data) {
                        case 0:
                            break
                        case 1:
                            this.$message.warning(res.data.msg)
                            break
                    }
                }
            })
            this.updateDialogVisible = true
        },
        updateBomNum() {
            if (this.editItemUsages.usages == '') return this.$message.warning('用量不能为空嗷')
            if (this.editItemUsages.usages <= 0) return this.$message.warning('用量必须为正数')
            updateBomUsages(JSON.stringify(this.editItemUsages)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('保存成功')
                    this.updateDialogVisible = false
                }
            })
        },
        // 删除当前物料标签
        showRemoveCurrentMat(node, current) {
            this.deleteCurrentMatId.batchName = current.batchName
            this.deleteCurrentMatId.rootCode = current.parentCode
            this.deleteCurrentMatId.parentCode = current.parentCode
            this.deleteCurrentMatId.matCode = current.matCode
            this.deleteCurrentMatId.currentCode = this.currentBh
            this.currentDelData = current
            checkIsUsed(JSON.stringify({ rootCode: this.currentBh })).then(res => {
                if (res.data.code == 0) {
                    switch (res.data.data) {
                        case 0:
                            break
                        case 1:
                            this.$message.warning(res.data.msg)
                            break
                    }
                }
            })
            this.delDialogVisible = true
        },
        removeCurrentMat() {
            deleteBomUsages(JSON.stringify(this.deleteCurrentMatId)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('删除成功')
                    this.delDialogVisible = false
                    this.currentIconCode = ''
                    this.getCurrentBomTree()
                }
            })
        },
        // 编码相关
        switchAddMode() {
            this.getMatEnCode()
            this.addItemDialogVisible = true
        },
        addItemDlogClose() {
            this.matEncode.level = 1
            this.matEncode.type = ''
            this.matEncode.parentNode = ''
            this.getMatEnCode()
            this.firstValue = ''
            this.showParamFlag = false
            this.addLiveType.name = ''
            this.addLiveType.bh = ''
            this.addLiveType.color = ''
            this.addLiveType.spec = ''
            this.addLiveType.brand = ''
            this.addLiveType.type = ''
            this.addLiveType.up = ''
            this.addLiveType.unit = ''
            this.addLiveType.remark = ''
            this.addLiveType.stk = ''
            this.addLiveType.pictureUrl = ''
            if (this.firClickNode !== '') {
                this.$refs[this.firClickNode][0].style.background = '#fff'
                this.$refs[this.firClickNode][0].style['color'] = '#606266'
            }
            this.firClickNode = ''
            this.secClickNode = ''
            this.thrClickNode = ''
            this.fourClickNode = ''
            this.fifClickNode = ''
        },
        getMatEnCode() {
            getMatCode(JSON.stringify(this.matEncode)).then(res => {
                if (res.data.code == 0) {
                    switch (this.matEncode.level) {
                        case 1:
                            this.secondFlag = false;
                            this.thirdFlag = false;
                            this.fourthFlag = false;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.firstColumn = res.data.data;
                            break;
                        case 2:
                            this.secondFlag = true;
                            this.thirdFlag = false;
                            this.fourthFlag = false;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.secondColumn = res.data.data;
                            break;
                        case 3:
                            this.thirdFlag = true;
                            this.fourthFlag = false;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.thirdColumn = res.data.data;
                            break;
                        case 4:
                            this.fourthFlag = true;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.fourthColumn = res.data.data;
                            break;
                        case 5:
                            this.fifFlag = true
                            if (this.matEncode.type == 'wrapper') this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.fifColumn = res.data.data;
                            break;
                    }
                }
            })
        },
        getNextEncode(seleteType, node, level, type) {
            this.showParamFlag = false
            switch (seleteType) {
                case 1:
                    // this.keyValue = this.firstValue.split('-')[0]
                    // this.matEncode.parentNode = this.firstValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.firstValue.split('-')[1]) + 1
                    // this.matEncode.type = this.firstValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.firClickNode !== node && this.firClickNode !== '') {
                        this.$refs[this.firClickNode][0].style.background = '#fff'
                        this.$refs[this.firClickNode][0].style['color'] = '#515775'
                    }
                    if (this.secClickNode !== node && this.secClickNode != '') {
                        this.$refs[this.secClickNode][0].style.background = '#fff'
                        this.$refs[this.secClickNode][0].style['color'] = '#515775'
                    }
                    this.thirdFlag = false;
                    this.fourthFlag = false;
                    this.fifFlag = false;
                    this.keyValue = node
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.secondParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.secondValue = ''
                    this.thirdValue = ''
                    this.fourthValue = ''
                    this.fifValue = ''
                    this.createLastNode = node
                    this.firClickNode = node
                    this.secClickNode = ''
                    this.thrClickNode = ''
                    this.fourClickNode = ''
                    this.fifClickNode = ''
                    // this.createLastNode = this.firstValue.split('-')[0]
                    break
                case 2:
                    // this.matEncode.parentNode = this.secondValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.secondValue.split('-')[1]) + 1
                    // this.matEncode.type = this.secondValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.secClickNode !== node && this.secClickNode !== '') {
                        this.$refs[this.secClickNode][0].style.background = '#fff'
                        this.$refs[this.secClickNode][0].style['color'] = '#515775'
                    }
                    if (this.thrClickNode !== node && this.thrClickNode !== '') {
                        this.$refs[this.thrClickNode][0].style.background = '#fff'
                        this.$refs[this.thrClickNode][0].style['color'] = '#515775'
                    }
                    this.fourthFlag = false;
                    this.fifFlag = false;
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.thirdParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.thirdValue = ''
                    this.fourthValue = ''
                    this.createLastNode = node
                    this.secClickNode = node
                    this.thrClickNode = ''
                    this.fourClickNode = ''
                    this.fifClickNode = ''
                    // this.createLastNode = this.secondValue.split('-')[0]
                    this.fifValue = ''
                    break
                case 3:
                    // this.matEncode.parentNode = this.thirdValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.thirdValue.split('-')[1]) + 1
                    // this.matEncode.type = this.thirdValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.thrClickNode !== '' && this.thrClickNode !== node) {
                        this.$refs[this.thrClickNode][0].style.background = '#fff'
                        this.$refs[this.thrClickNode][0].style['color'] = '#515775'
                    }
                    if (this.fourClickNode !== '' && this.fourClickNode !== node) {
                        this.$refs[this.fourClickNode][0].style.background = '#fff'
                        this.$refs[this.fourClickNode][0].style['color'] = '#515775'
                    }
                    this.fifFlag = false;
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.fourthParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.fourthValue = ''
                    this.createLastNode = node
                    this.thrClickNode = node
                    this.fourClickNode = ''
                    this.fifClickNode = ''
                    // this.createLastNode = this.thirdValue.split('-')[0]
                    this.fifValue = ''
                    break
                case 4:
                    // this.matEncode.parentNode = this.fourthValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.fourthValue.split('-')[1]) + 1
                    // this.matEncode.type = this.fourthValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.fourClickNode !== '' && this.fourClickNode !== node) {
                        this.$refs[this.fourClickNode][0].style.background = '#fff'
                        this.$refs[this.fourClickNode][0].style['color'] = '#515775'
                    }
                    if (this.fifClickNode !== '' && this.fifClickNode !== node) {
                        this.$refs[this.fifClickNode][0].style.background = '#fff'
                        this.$refs[this.fifClickNode][0].style['color'] = '#515775'
                    }
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.fifthParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.createLastNode = node
                    this.fourClickNode = node
                    this.fifClickNode = ''
                    // this.createLastNode = this.fourthValue.split('-')[0]
                    this.fifValue = ''
                    break
                case 5:
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.fifClickNode !== '' && this.fifClickNode !== node) {
                        this.$refs[this.fifClickNode][0].style.background = '#fff'
                        this.$refs[this.fifClickNode][0].style['color'] = '#515775'
                    }
                    this.createLastNode = node
                    this.fifClickNode = node
                    // this.createLastNode = this.fifValue.split('-')[0]
                    this.isAddFlag = false
                    break
            }
        },
        addDataShow(level) {
            switch (level) {
                case 1:
                    this.addRootCode.level = level;
                    this.addBaseDialogVisible = true
                    break;
                case 2:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.secondParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.secondParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
                case 3:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.thirdParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.thirdParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
                case 4:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.fourthParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.fourthParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
                case 5:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.fifthParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.fifthParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
            }
        },
        addBaseDlogClose(form) {
            this.$refs[form].resetFields();
        },
        addMatEncode(form) {
            this.$refs[form].validate((valid) => {
                if (!valid) return;
                addParMatCode(JSON.stringify(this.addRootCode)).then(res => {
                    if (res.data.code == 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('newmaterialcategoryadded')
                        })
                        this.addDialogVisible = false
                        this.matEncode.parentNode = this.addRootCode.parentNode
                        this.matEncode.level = this.addRootCode.level
                        this.getMatEnCode()
                    }
                })
            })
        },
        addBaseMatEncode(form) {
            this.$refs[form].validate((valid) => {
                if (!valid) return;
                addParMatCode(JSON.stringify(this.addBaseRootCode)).then(res => {
                    if (res.data.code == 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('newmaterialcategoryadded')
                        })
                        this.addBaseDialogVisible = false
                        this.matEncode.parentNode = this.addBaseRootCode.parentNode
                        this.matEncode.level = this.addBaseRootCode.level
                        this.getMatEnCode()
                    }
                })
            })
        },
        createCode() {
            updateParMatCode(JSON.stringify({ node: this.createLastNode })).then(res => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: this.$t('creatingasuccessful')
                    })
                    this.showParamFlag = true
                    this.addLiveType.bh = res.data.data
                    this.addLiveType.type = this.matEncode.type
                    this.isAddFlag = true
                }
            })
        },
        // 图片上传处理
        scotland() {
            this.$message.warning('只能选择一个文件')
        },
        handleImg(res) {
            if (res.code == 0) {
                this.addLiveType.pictureUrl = res.data
            }
        },
        hadleRemove(file) {
            if (file) {
                this.addLiveType.pictureUrl = ''
            }
        },
        saveModeCode() {
            insertMatDetail(JSON.stringify(this.addLiveType)).then(res => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: this.$t('sucadd')
                    })
                    this.addMode = false
                    this.addItemDialogVisible = false
                    this.matlistFabDialogVisible = false
                    switch (this.addOrCite) {
                        case 'add':
                            this.addBomMatForm.matCode = this.addLiveType.bh
                            this.addMatItemName = this.addLiveType.bh
                            break
                        case 'cite':
                            this.citeBomMatForm.matCode = this.addLiveType.bh
                            this.citeMatItemName = this.addLiveType.bh
                            break
                        default:
                            this.citeNextBomMatForm.matCode = this.addLiveType.bh
                            this.citeMatItemName = this.addLiveType.bh
                            break
                    }
                }
            })
        },
        jayCheck() {
            this.matlistFabDialogVisible = false
        },
        // 导出
        showExport() {
            let itemBasic = JSON.parse(sessionStorage.getItem('matCodeItem'))
            this.bomExport.bh = itemBasic.bh
            this.bomExport.name = itemBasic.name
            this.bomExport.spec = itemBasic.spec
            this.bomExport.color = itemBasic.color
            this.exportDialogVisible = true
        },
        exportData() {
            bomExcel(JSON.stringify(this.bomExport)).then(res => {
                if (res.status == 200) {
                    let date = new Date()
                    let name = "bom" + date.getFullYear() + (date.getMonth() + 1) + date.getDate() + ".xls"
                    let blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'})
                    let url = window.URL.createObjectURL(blob);
                    let aLink = document.createElement("a");
                    aLink.style.display = 'none';
                    aLink.href = url;
                    aLink.setAttribute('download', name);
                    document.body.appendChild(aLink);
                    aLink.click();
                    document.body.removeChild(aLink); //下载完成移除元素
                    window.URL.revokeObjectURL(url); //释放掉blob对象
                    this.exportDialogVisible = false
                    this.$message.success('导出成功')
                }
            })
        },
        showAuditItem() {
            this.auditCommitForm.bomCode = this.currentBh
            this.auditDialogVisible = true
        },
        auditItem() {
            updateReviewStatus(JSON.stringify(this.auditCommitForm)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('审核成功')
                    this.auditDialogVisible = false
                }
            })
        },
        showCiteCurrent() {
            checkIsUsed(JSON.stringify({ rootCode: this.currentBh })).then(res => {
                if (res.data.code == 0) {
                    switch (res.data.data) {
                        case 0:
                            break
                        case 1:
                            this.$message.warning(res.data.msg)
                            break
                    }
                }
            })
            if (this.currentClickMenu.matCode) {
                this.citeBomMatForm.rootCode = this.currentClickMenu.matCode
                this.citeBomMatForm.parentCode = this.currentClickMenu.matCode
                this.citeBomMatForm.currentCode = this.currentBh
            } else {
                this.citeBomMatForm.rootCode = this.currentBh
                this.citeBomMatForm.parentCode = this.currentBh
                this.citeBomMatForm.currentCode = this.currentBh
            }
            this.citeBomMatForm.level = 1
            this.addOrCite = 'cite'
            this.showMatListDlog()
        },
        citeDlogClose() {
            this.citeMatItemName = ''
            this.citeBomMatForm.usages = ''
        },
        citeData() {
            if (this.citeMatItemName == '') return this.$message.warning('物料不能为空')
            if (this.citeBomMatForm.usages == '') return this.$message.warning('用量不能为空')
            importClotheAndPants(JSON.stringify(this.citeBomMatForm)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('引用成功')
                    this.citeBomMatDialogVisible = false
                    this.getCurrentBomTree()
                }
            })
        },
        showCancelCurrentMat(bindId) {
            this.revocationUuid.uuid = bindId
            this.revocationUuid.parentBh = this.currentBh
            this.revocationDialogVisible = true
        },
        revocationCurrentMat() {
            cancelBind(JSON.stringify(this.revocationUuid)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('取消引用成功')
                    this.revocationDialogVisible = false
                }
            })
        },
        showCiteNextCurrent() {
            checkIsUsed(JSON.stringify({ rootCode: this.currentBh })).then(res => {
                if (res.data.code == 0) {
                    switch (res.data.data) {
                        case 0:
                            break
                        case 1:
                            this.$message.warning(res.data.msg)
                            break
                    }
                }
            })
            if (this.currentClickMenu.matCode) {
                this.citeNextBomMatForm.rootCode = this.currentClickMenu.matCode
                this.citeNextBomMatForm.currentCode = this.currentBh
            } else {
                this.citeNextBomMatForm.rootCode = this.currentBh
                this.citeNextBomMatForm.currentCode = this.currentBh
            }
            this.addOrCite = 'import'
            this.showMatListDlog()
        },
        showBomListDlog() {
            this.seachCiteBomData()
            this.bomlistFabDialogVisible = true
        },
        citeNextDlogClose(form) {
            this.citeMatItemName = ''
            this.$refs[form].resetFields()
        },
        citeNextData() {
            if (this.citeMatItemName == '') return this.$message.warning('物料不能为空')
            importSimilarMatBom(JSON.stringify(this.citeNextBomMatForm)).then(res => {
                if (res.data.code == 0) {
                    this.$message.success('引用成功')
                    this.citeNextBomMatDialogVisible = false
                    this.getCurrentBomTree()
                }
            })
        },
        getCiteBomData() {
            this.seachBomForm.pageNum = 1
            this.seachCiteBomData()
        },
        seachCiteBomData() {
            getCodeRuleAll(JSON.stringify(this.seachBomForm)).then(res => {
                if (res.data.code == 0) {
                    this.bomTotal = res.data.data.hits
                    res.data.data.data.forEach(item => {
                        switch (item.reviewStatus) {
                            case 0:
                                item.reviewStatus = '未审核'
                                break
                            case 1:
                                item.reviewStatus = '已审核'
                                break
                        }
                    })
                    this.bomTab = res.data.data.data
                }
            })
        },
        handleBomSizeChange (newSize) {
            this.seachBomForm.pageSize = newSize
            this.seachCiteBomData()
        },
        handleBomCurrentChange (newNum) {
            this.seachBomForm.pageNum = newNum
            this.seachCiteBomData()
        },
        coleCheck() {
            this.bomlistFabDialogVisible = false
        },
        handleBomIdChange(row) {
            if (!row) return
            switch (this.addOrCite) {
                case 'add':
                    this.addBomMatForm.matCode = row.bh
                    this.addMatItemName = row.bh
                    break
                case 'cite':
                    this.citeBomMatForm.matCode = row.bh
                    this.citeMatItemName = row.bh
                    break
                default:
                    this.citeNextBomMatForm.matCode = row.bh
                    this.citeMatItemName = row.bh
                    break
            }
        }
    },
    mounted() {
        this.viewOrEdit = sessionStorage.getItem('matCodeType')
        this.currentItem = JSON.parse(sessionStorage.getItem('matCodeItem'))
        this.currentBh = this.currentItem.bh
        this.currentName = this.currentItem.name
        this.currentUuid = this.currentItem.uuid
        this.currentStatus = this.currentItem.reviewStatus
        this.getCurrentBomTree()
    }
}
</script>

<style lang="less" scoped>
@import './secBomMsg.less';
</style>
